<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>学员信息管理</title>
    <style>
        * {
            margin: auto 0px;
        }
    </style>
    <%--如果作用域中list为null则跳转到/student/list--%>
    <c:if test="${list == null}">
        <script>
            window.location.href = "${pageContext.request.contextPath }/student/list";
        </script>
    </c:if>
</head>
<body>
<div align="center">
    <h1>学员信息列表</h1>
    <p style=" width: 700px; text-align: right"><span><a href="${pageContext.request.contextPath }/student/add">添加学员</a></span></p>
    <table border="1" width="700px" align="center">
        <tr style="background-color: #666">
            <td>学员编号</td>
            <td>学员姓名</td>
            <td>学员性别</td>
            <td>学员年龄</td>
            <td>家庭住址</td>
            <td>Email</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${list}" var="stu" varStatus="indexStatus">
            <tr ${indexStatus.index % 2 == 1 ? "style=\"background-color: #ccc\"" : ""}>
                <td>${stu.sid}</td>
                <td><a href="${pageContext.request.contextPath }/student/update/${stu.sid}">${stu.sname}</a></td>
                <td>${stu.sgender}</td>
                <td>${stu.sage}</td>
                <td>${stu.saddress}</td>
                <td>${stu.semail}</td>
                <td><a href="javascript:del(${stu.sid});">删除</a></td>
            </tr>
        </c:forEach>
    </table>
</div>
<script src="${pageContext.request.contextPath }/statics/js/jquery-1.12.4.js"></script>
<script>
    function del(sid) {
        if (confirm("确认要删除该学员信息？")) {
            $.ajax({
                url: "${pageContext.request.contextPath }/student/del",
                type: "POST",
                data: "sid=" + sid,
                dataType: "json",
                success: function (data) {
                    if (data.isok) {
                        alert("删除成功！");
                        window.location.href = "${pageContext.request.contextPath }/student/list";
                    } else {
                        alert("删除失败！");
                    }
                }
            });
        }
    }
</script>
</body>
</html>